<template>
  <!--渠道商列表-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" icon="el-icon-plus" @click="add()">
          新增
        </el-button>
        <el-button type="primary" icon="el-icon-download" @click="handleExport">
          导出
        </el-button>
        <el-upload
          class="uploads"
          action=""
          ref="upload_file"
          list-type="fileList"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove3"
          :http-request="handleExport2"
          :show-file-list="false"
          :limit="1"
        ><el-button type="primary" icon="el-icon-upload2" style="margin-bottom: 10px" >导入</el-button>
        </el-upload>

      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
<!--          <el-form-item>
            <div style="width: 80px">渠道商:</div>
          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              style="margin-right: 20px"
              placeholder="请输入渠道商"
              clearable
            />
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">渠道状态：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              clearable
              placeholder="请选择渠道状态"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="restart"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>

    </vab-query-form>

    <el-table
      id="out-table"
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="channel_name"
        label="渠道商"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="proportion_rate"
        label="分润比例"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="proportion_total_money"
        label="历史分润记录"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="phone_number"
        label="手机号"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="status_text"
        label="渠道状态"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_time"
        label="创建日期"
      />
      <el-table-column align="center" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope)">编辑</el-button>
          <el-button v-if="false" type="text" @click="pics(scope.row)">
            完税证明
          </el-button>
          <el-dropdown style="color: #1890ff">
            <span class="el-dropdown-link">
              更多
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="edit(scope)">
                详情
              </el-dropdown-item>
              <el-dropdown-item @click.native="delList(scope.row)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 弹窗 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="120px">
        <el-form-item label="渠道商:" required>
          <el-input v-model="form.channel_name" :rows="15" />
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="form.password" :rows="15" />
        </el-form-item>
        <el-form-item label="手机号:">
          <el-input v-model="form.phone_number" :rows="15" />
        </el-form-item>
        <el-form-item label="分润比例:">
          <el-input v-model="form.proportion_rate" :rows="15" />
        </el-form-item>
        <el-form-item label="渠道状态:">
          <el-select
            v-model="form.status"
            placeholder="请选择渠道状态"
            clearable
          >
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getChannelAccountLists,
    saveChannelAccounts,
    delChannelAccounts,
    importExcelChannelAccounts
  } from '@/api/common'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'
  import axios from "axios";

  export default {
    name: 'Statistics',
    data() {
      return {
        status: [
          {
            value: 1,
            label: '停用',
          },
          {
            value: 2,
            label: '启用',
          },
        ],
        queryForm: {
          name: '',
          statu: '',
        },
        form: {
          channel_name: '',
          password: '',
          phone_number: '',
          proportion_rate: '',
          status: '',
        },
        disabled: false,
        listLoading: false,
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        wintit: '',
        memberid: '',
        dialogFormVisible: false,
        addtype: false,
        edittype: false,
        file:'',
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleExport2(params) {
        console.log(123,params)
        let file = new FormData()
        file.append('file', params.file)
        importExcelChannelAccounts(file).then(res=>{
          console.log(1111,res)
          if(res.code === 2000){
            this.$message.success('导入成功！')
            this.getData()
          }
        })
        this.$refs.upload_file.clearFiles();

        /*const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.file = res.data.host + '/' + res.data.dir + file.name
            console.log(66, this.file.file)
          })
        })*/
      },
      handlePictureCardPreview(){},
      handleRemove3(){},
      setSelectRows(){},
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.getData()
      },
      //导出
      handleExport() {
        /* 从表生成工作簿对象 */
        var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
        /* 获取二进制字符串作为输出 */
        var wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array',
        })
        try {
          FileSaver.saveAs(
            //Blob 对象表示一个不可变、原始数据的类文件对象。
            //Blob 表示的不一定是JavaScript原生格式的数据。
            //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
            //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
            new Blob([wbout], { type: 'application/octet-stream' }),
            //设置导出文件名称
            'outsheet.xlsx',
          )
        } catch (e) {
          if (typeof console !== 'undefined') console.log(e, wbout)
        }
        return wbout
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          channel_name: this.queryForm.name,
          status: this.queryForm.status,
        }
        getChannelAccountLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      selectStatus(e) {
        this.queryForm.status === e.value
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.form = []
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.form = this.list[row.$index]
        this.memberid = row.row.id
      },
      send() {
        let params = {
          channel_name: this.form.channel_name,
          password: this.form.password,
          phone_number: this.form.phone_number,
          proportion_rate: this.form.proportion_rate,
          status: this.form.status,
        }
        saveChannelAccounts(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      save() {
        let params = {
          id: this.memberid,
          channel_name: this.form.channel_name,
          password: this.form.password,
          phone_number: this.form.phone_number,
          proportion_rate: this.form.proportion_rate,
          status: this.form.status,
        }
        saveChannelAccounts(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      delList(row) {
        let params = {
          id: row.id,
        }
        delChannelAccounts(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
    margin-right: 20px;
  }
</style>
